<?php

class AuthorizationController extends Controller {

    public $content = array();

    public function actionIndex() {
        echo 'abc';
		Yii::app()->end();
    }
	
	public function actionGetSamplePhotos(){
		$result = array();
		$mProfile = new mProfile();
		$seeking = isset($_POST['seeking'])?$_POST['seeking']:1;
		$minage = isset($_POST['minAge'])?$_POST['minAge']:1;
		$maxage = isset($_POST['maxAge'])?$_POST['maxAge']:100;
		$country = isset($_POST['countryCode'])?$_POST['countryCode']:'US';
		$postcode = isset($_POST['postalCode'])?$_POST['postalCode']:'';
		if($seeking == 1)
			$seeking = 'male';
		else
			$seeking = 'female';
			
		$user = $mProfile->getSamplePhotosInRegisterFree($seeking,$minage,$maxage,$country,$postcode);
		
		for($ii=0;$ii<count($user);$ii++){	

			$link_img = '';
			if ($user[$ii]['link_img'] != null) 
				$link_img = FRONT_SITE_URL.'www/uploads/photos/thumbs/thumb_'.$user[$ii]['link_img']; 
			else {
				$link_img = $user[$ii]['gender'] == 'male' ? FRONT_SITE_URL . 'www/images/images1/noPhoto-1.gif' : FRONT_SITE_URL . 'www/images/images1/noPhoto-2.gif';
			}

			$result[$ii]['BirthDate'] = calculateYearOld($user[$ii]['birthdate']);
			$result[$ii]['FirstName'] = null;
			$result[$ii]['Gender'] = $user[$ii]['gender'];
			$result[$ii]['LastName'] = null;
			$result[$ii]['AutoLoginUrl'] = null;
			$result[$ii]['BodyType'] = $user[$ii]['body_type'];
			$result[$ii]['ClientIP'] = null;
			$result[$ii]['Community'] = array('CommunityUrl'=>FRONT_SITE_URL,'Identifier'=>'21','Name'=>'bbpeoplemeet.com');
			$result[$ii]['Description'] = null;
			$result[$ii]['Education'] = $user[$ii]['education'];
			$result[$ii]['EmailAddress'] = null;
			$result[$ii]['EncodedIdentifier'] = null;
			$result[$ii]['Ethnicity'] = $user[$ii]['ethnicity'];
			$result[$ii]['EyeColor'] = $user[$ii]['eyes_color'];
			$result[$ii]['HairColor'] = $user[$ii]['hair_color'];
			$result[$ii]['HashedPassword'] = null;
			$result[$ii]['Height'] = null;
			$result[$ii]['Identifier'] = '2566263';
			$result[$ii]['Location'] = array('City'=>$user[$ii]['city'],'Country'=>array('Code'=>null,'Name'=>$user[$ii]['country'],'ShortCode'=>$user[$ii]['country']),'PostalCode'=>$user[$ii]['postcode'],'State'=>array('StateReference'=>array('CountryCode'=>null,'StateCode'=>null)));
			$result[$ii]['Looks'] = 0;
			$result[$ii]['MaritalStatus'] = $user[$ii]['marial_status'];
			$result[$ii]['NickName'] = $user[$ii]['username'];
			$result[$ii]['NumberOfChildren'] = $user[$ii]['children'];
			$result[$ii]['Password'] = null;
			$result[$ii]['PrimaryImage'] = array('ImageType'=>0, 'ImageUrl'=>$link_img,'IsPrimaryPhoto'=>true);
			$result[$ii]['ProfileImages'] = null;
			$result[$ii]['ProfileUrl'] = FRONT_SITE_URL.'user/profile/viewprofile?profile='.$user[$ii]['user_id'];
			$result[$ii]['Religion'] = $user[$ii]['religion'];
			$result[$ii]['SeekingGender'] = $user[$ii]['seeking'];
			$result[$ii]['SmokingPreference'] = null;
			$result[$ii]['Source'] = null;
			$result[$ii]['SurveyAnswer'] = null;
			$result[$ii]['TestingGUID'] = null;
			$result[$ii]['ZodiacSign'] = $user[$ii]['zodiac'];
			$result[$ii]['MobilePhoneNumber'] = null;
			$result[$ii]['AdOptIn'] = null;
		}
		
		echo CJSON::encode($result);
		Yii::app()->end();
	}
	
	public function actionCreateMemberCPV2(){		
		$result = array();
		$result['Fault'] = true;
		$result['text'] = array();
		$result['text'][0] = 'Unspecified error. Please contact us';
		if (r()->isPostRequest) {
            $userRegistration = new mRegistration('regisFree');
            $userRegistration->aliasAttrFormWithModel1($_POST);
            if ($userRegistration->validate() && $userRegistration->checkUniqueUsernameEmail()) {
				$userRegistration->insertUserFree($_POST['password']);
				Yii::app()->session['tmp_user_id'] = $userRegistration->id;
                $result['Fault'] = false;
				$result['CommunityMember'] = $userRegistration->id;
				$result['text'][0] = '';
            } else{
				$errores = $userRegistration->getErrors();
				//var_dump($errores);
				if(count($errores)>0){
					foreach($errores as $error){
						$result['text'][0] = $error[0];
					}
				}else
					$result['text'][0] = 'Duplicate Username and Email';
			}
        }
		echo CJSON::encode($result);
		Yii::app()->end();
	}
	
	public function actionGetQuestionsAndResponses(){
		$idquestion = array(19,101,4,5,16,17,98,126,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1);
		$constant = array(Constant::getTallArray(),Constant::getEthnicityArray(),Constant::getBodyArray(),Constant::getMaritalArray(),Constant::getChildArray(),Constant::getChildArray(),Constant::getSmokerArray(),Constant::getSmokerArray(),Constant::getSmokerArray(),Constant::getSmokerArray(),Constant::getSmokerArray(),Constant::getSmokerArray(),Constant::getSmokerArray(),Constant::getSmokerArray(),Constant::getSmokerArray(),Constant::getSmokerArray());
		for($ii=0;$ii<=14;$ii++){
			$result[$ii]['IsSignupQuestion'] = true;
			$result[$ii]['QuestionID'] = $idquestion[$ii];
			$result[$ii]['QuestionText'] = 'Hair Color';
			$result[$ii]['Required'] = false;
			$ii1 = 0;
			foreach($constant[$ii] as $key => $value){
				$result[$ii]['Responses'][$ii1]['ResponseID'] = $key;
				$result[$ii]['Responses'][$ii1]['ResponseText'] = $value;
				$ii1++;
			}
		}
		echo CJSON::encode($result);
		Yii::app()->end();
	}
	
	public function actionUpdateQuestionsAndResponsesForMemberBasic(){
		$result = array();
		$result[0] = 'error';
		if (r()->isPostRequest && isset($_POST['memberID']) && Yii::app()->session['tmp_user_id']==$_POST['memberID']) {
			$userRegistration = new mRegistration();
			$userRegistration = $userRegistration->findByPk($_POST['memberID']);
			if($userRegistration != null){
				$questionResponse = $_POST['questionResponses'];
				$questionResponse = explode(',',$questionResponse );
				$saveQuestion = array();
				if(count($questionResponse)>0){
					$count = 0;
					foreach($questionResponse as $question){
						$value = explode('=',$question);
						$saveQuestion[$count] = isset($value[1])?$value[1]:0;
						$count++;
					}					
					$userRegistration->id = $_POST['memberID'];
					$userRegistration->tall = $saveQuestion[0] * 30.48;
					$userRegistration->ethnicity = $saveQuestion[1];
					$userRegistration->bodyType = $saveQuestion[2];
					$userRegistration->marialStatus = $saveQuestion[3];
					$userRegistration->childrenHave = $saveQuestion[4];
					$userRegistration->childrenLiving = $saveQuestion[5];
					$userRegistration->smoke = $saveQuestion[6];
					//$userRegistration->updateProfileUserFree();
				}
				$result[0] = 'success';
			}
		}
		echo CJSON::encode($result);
		Yii::app()->end();
	}
}
